package com.jkoss.wine.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.system.entity.Department;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.service.IDepartmentService;
import com.jkoss.wine.system.service.IRoleService;
import com.jkoss.wine.system.vo.DepartmentVo;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

/**
 * 
 * @Author 周华军
 * @Version 1.0, 2018-09-25
 * @See
 * @Since com.jkoss.wine.system.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/system/department")
public class DepartmentController extends BaseController {

	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IRoleService iRoleService;

	/**
	 * 列表
	 * 
	 * @param name
	 * @param dwzPageBean
	 * @param sort
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/system/department/list")
	public String list(String name, String sort, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		String string = request.getParameter("org2.name");
		// 前端框架bean
		dwzPageBean.getCountResultMap().put("name", string);
		// 构建排序条件 是否删除：0-未删除、1-已删除、2-不能删除
		Wrapper wrapper = Condition.create().ne("del", 1).like("name", string);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("add_time", false);
		}
		Page resultPage = iDepartmentService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "system/department/list";
	}

	/**
	 * 去新增
	 * 
	 * @param department
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsert")
	@RequiresPermissions("/system/department/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {
		return "system/department/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/system/department/toInsert")
	@ResponseBody
	public Object insert(@Valid Department department, HttpServletRequest request, HttpServletResponse response) {

		// 判断部门是否存在
		Wrapper wrapper = Condition.create().eq("name", department.getName());
		Department department2 = iDepartmentService.selectOne(wrapper);
		if (!CommonUtil.isBlank(department2) && department2.getDel() == 0) {
			return ajaxError("部门已存在");
		}
		// if (!CommonUtil.isBlank(department2) && department2.getDel() == 1) {
		// department2.setDel(0);
		// department2.setAddTime(CommonUtil.getDate());
		// MerchantEmployees employees = (MerchantEmployees)
		// request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		// department2.setOperator(employees.getEmployeeId());
		// if (iDepartmentService.updateById(department2)) {
		// return ajaxSuccess();
		// } else {
		// return ajaxError();
		// }
		// }
		// 是否删除：0-未删除、1-已删除、2-不能删除
		department.setDel(0);
		// 添加时间
		department.setAddTime(CommonUtil.getDate());
		// 获取当前操作人
		MerchantEmployees employees = (MerchantEmployees) request.getSession().getAttribute(Constant.SESSION_USER_KEY);
		department.setOperator(employees.getEmployeeId());
		if (iDepartmentService.insert(department)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	/**
	 * 去修改
	 * 
	 * @param id
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toUpdate")
	@RequiresPermissions("/system/department/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iDepartmentService.selectById(id));
		return "system/department/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/system/department/toUpdate")
	@ResponseBody
	public Object update(@Valid Department department, HttpServletRequest request, HttpServletResponse response) {
		// // 判断部门是否存在
		// Wrapper wrapper = Condition.create().eq("name", department.getName());
		// Department department2 = iDepartmentService.selectOne(wrapper);
		// if (!CommonUtil.isBlank(department2) && department2.getDel() == 1) {
		// return ajaxError("部门已存在");
		// }
		// 判断部门是否存在
		Wrapper wrapper = Condition.create().eq("name", department.getName());
		Department department2 = iDepartmentService.selectOne(wrapper);
		if (!CommonUtil.isBlank(department2)) {
			return ajaxError("部门名称已存在");
		}
		if (iDepartmentService.updateById(department)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 去删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("/system/department/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			Department department = new Department();
			department.setId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			department.setDel(1);
			list.add(department);
		}
		if (iDepartmentService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 查询部门名称
	 * 
	 * @param id
	 * @param shop
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getDepartmentName")
	@ResponseBody
	public Object getproductTypeName(String name, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("name", name).ne("del", 1);
		List<Department> list = iDepartmentService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出部门管理Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/Departmentexport")
	@RequiresPermissions("/system/department/Departmentexport")
	public String Departmentexport(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("del", 1);
		// 根据用户id获取角色
		// boolean isDirector = true;
		// List<Role> roles = getRoleList();
		// for (Role role : roles) {
		// if ("运营主管".equals(role.getName())) {
		// isDirector = false;
		// }
		// }
		// if (isDirector) {
		// wrapper.eq("maid", getUserId());
		// }
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iDepartmentService.selectVoByPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "部门管理", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, Department.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "部门管理");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 部门详情
	 * 
	 * @param orid
	 * @param map
	 * @return
	 */
	@RequestMapping("/details")
	@RequiresPermissions("/system/department/details")
	public String details(String dmid, DwzPageBean dwzPageBean, ModelMap map) {
		// 部门信息
		Wrapper dmwrapper = Condition.create().ne("del", 1).eq("id", dmid);
		DepartmentVo result = iDepartmentService.selectVoByOne(dmwrapper);
		map.put("result", result);
		// 部门岗位
		Wrapper rwrapper = Condition.create().ne("r.del", 1).eq("r.dmid", dmid);
		Page resultPage = iRoleService.selectVoByPage(dwzPageBean.toPage(), rwrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "system/department/details";
	}
}
